﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using COM.OA.Entity;
using COM.OA.BLL;
using System.Collections.Generic;

public partial class rlzy_kqtj : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        users loginuser = Session["loginuser"] as users;
        if (loginuser == null)
        {
            this.Response.Write(string.Format(GetRedirect.ALLREDIRECT, "../login.aspx"));
        }
        else
        {
            string s = this.Request["id"];
            int id = Int32.Parse(s);
            if (id == 1)
            {
                this.shangwu();
            }
            if (id == 2)
            {
                this.xiawu();
            }
        }
      
    }

    protected void shangwu()
    {
        //先查询 有没有添加过上午考勤
         COM.OA.Entity.users loginuser =Session["loginuser"] as users;
         if (loginuser == null)
         {
             this.Response.Write(string.Format(GetRedirect.REDIRECT, "../login.aspx"));
         }
         else
         {
             int id = loginuser.u_id;
             string where = "att_u_id=" + id;
             IList<attendance> ilist = attendanceBLL.Select(where);
             if (ilist.Count == 0)//如果为空 就直接添加
             {
                 DateTime riqi = DateTime.Now;
                 DateTime shangwu = DateTime.Now;
                 DateTime xiawu = DateTime.Parse("1753-01-01");
                 attendance att = new attendance();
                 att.att_date = riqi;
                 att.att_forenoontime = shangwu;
                 att.att_afternoontime = xiawu;
                 att.att_u_id = loginuser.u_id;
                 att.att_dept_id = loginuser.u_dept_id;
                 attendanceBLL.Insert(att);
                 this.Response.Write(string.Format(GetRedirect.WINALERT, "上午添加成功!"));
                 this.Response.Write(string.Format(GetRedirect.REDIRECT, "aaaa.aspx"));
             }
             else
             {
                 List<attendance> list = (List<attendance>)ilist;
                 attendance at = new attendance();
                 at = list[0];
                 string zuihou = at.att_forenoontime.ToString("yyyy-MM-dd");//数据库最后一天记录的日期
                 string xianzai = DateTime.Now.ToString("yyyy-MM-dd");//今天的日期
                 if (xianzai.Equals(zuihou))//相同最后日期 与今天日期相同? 相同不添加 不同添加 
                 {
                     this.Response.Write(string.Format(GetRedirect.REDIRECT, "aaaa.aspx"));
                 }
                 else
                 {
                     DateTime riqi = DateTime.Now;
                     DateTime shangwu = DateTime.Now;
                     DateTime xiawu = DateTime.Parse("1753-01-01");
                     attendance att = new attendance();
                     att.att_date = riqi;
                     att.att_forenoontime = shangwu;
                     att.att_afternoontime = xiawu;
                     att.att_u_id = loginuser.u_id;
                     att.att_dept_id = loginuser.u_dept_id;
                     attendanceBLL.Insert(att);
                     this.Response.Write(string.Format(GetRedirect.WINALERT, "上午添加成功!"));
                     this.Response.Write(string.Format(GetRedirect.REDIRECT, "aaaa.aspx"));
                 }
             }




         }

    }

    protected void xiawu()
    {
        COM.OA.Entity.users loginuser = Session["loginuser"] as COM.OA.Entity.users;
        if (loginuser == null)
        {
            this.Response.Write(string.Format(GetRedirect.REDIRECT, "../login.aspx"));
        }
        else
        {
            int id = loginuser.u_id;
            string where = "att_u_id=" + id;
            IList<attendance> ilist = attendanceBLL.Select(where);
            if (ilist.Count == 0)//如果没有记录直接添加
            {
                DateTime riqi = DateTime.Now;
                DateTime shangwu = DateTime.Parse("1753-01-01");
                DateTime xiawu = DateTime.Now;
                attendance att = new attendance();
                att.att_date = riqi;
                att.att_forenoontime = shangwu;
                att.att_afternoontime = xiawu;
                att.att_u_id = loginuser.u_id;
                att.att_dept_id = loginuser.u_dept_id;
                attendanceBLL.Insert(att);
                this.Response.Write(string.Format(GetRedirect.WINALERT, "下午添加成功!!"));
                this.Response.Write(string.Format(GetRedirect.REDIRECT, "aaaa.aspx"));
            }
            else
            {
                List<attendance> list = (List<attendance>)ilist;
                attendance at = new attendance();
                at = list[0];
                string date = at.att_date.ToString("yyyy-MM-dd");
                string jintian = DateTime.Now.ToString("yyyy-MM-dd");
                attendance att = new attendance();
                if (date.Equals(jintian))//有记录判断是不是 今天日期 是修改成现在时间 不是就添加
                {
                    att.att_afternoontime = DateTime.Now;
                    att.att_date = at.att_date;
                    att.att_dept_id = at.att_dept_id;
                    att.att_id = at.att_id;
                    att.att_u_id = at.att_u_id;
                    att.att_forenoontime = at.att_forenoontime;
                    attendanceBLL.Update(att);
                    this.Response.Write(string.Format(GetRedirect.WINALERT, "下午添加成功!"));
                    this.Response.Write(string.Format(GetRedirect.REDIRECT, "aaaa.aspx"));

                }
                else
                {
                    DateTime riqi = DateTime.Now;
                    DateTime shangwu = DateTime.Parse("1753-01-01");
                    DateTime xiawu = DateTime.Now;
                    att.att_date = riqi;
                    att.att_forenoontime = shangwu;
                    att.att_afternoontime = xiawu;
                    att.att_u_id = loginuser.u_id;
                    att.att_dept_id = loginuser.u_dept_id;
                    attendanceBLL.Insert(att);
                    this.Response.Write(string.Format(GetRedirect.WINALERT, "下午添加成功!!"));
                    this.Response.Write(string.Format(GetRedirect.REDIRECT, "aaaa.aspx"));

                }
            }
           

        }
    }
}
